package com.yxinsur.imc.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcComm;
import com.yxinsur.imc.business.domain.ImcCommRisk;
import com.yxinsur.imc.business.form.CommRiskForm;
import com.yxinsur.imc.business.mapper.ImcCommMapper;
import com.yxinsur.imc.business.mapper.ImcCommRiskMapper;
import com.yxinsur.imc.business.mapper.ImcOrderRiskMapper;
import com.yxinsur.imc.business.service.IImcCommRiskService;
import com.yxinsur.imc.business.vo.CommRiskVo;
import com.yxinsur.imc.business.vo.OrderRiskToCommRiskVo;
import com.yxinsur.imc.common.exception.BaseException;
import com.yxinsur.imc.common.utils.DateUtils;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 佣金计算险种明细Service业务层处理
 *
 * @author imc
 * @date 2021-05-26
 */
@Service
public class ImcCommRiskServiceImpl extends ServiceImpl<ImcCommRiskMapper, ImcCommRisk> implements IImcCommRiskService {
    @Autowired
    private ImcCommRiskMapper imcCommRiskMapper;
    @Autowired
    private ImcOrderRiskMapper imcOrderRiskMapper;
    @Autowired
    private ImcCommMapper imcCommMapper;

    /**
     * 查询佣金计算险种明细
     *
     * @param id 佣金计算险种明细ID
     * @return 佣金计算险种明细
     */
    @Override
    public ImcCommRisk selectImcCommRiskById(Long id) {
        return imcCommRiskMapper.selectImcCommRiskById(id);
    }

    /**
     * 查询佣金计算险种明细列表
     *
     * @param imcCommRisk 佣金计算险种明细
     * @return 佣金计算险种明细
     */
    @Override
    public List<ImcCommRisk> selectImcCommRiskList(ImcCommRisk imcCommRisk) {
        return imcCommRiskMapper.selectImcCommRiskList(imcCommRisk);
    }

    /**
     * 新增佣金计算险种明细
     *
     * @param imcCommRisk 佣金计算险种明细
     * @return 结果
     */
    @Override
    public int insertImcCommRisk(ImcCommRisk imcCommRisk) {
        imcCommRisk.setCreateTime(DateUtils.getNowDate());
        return imcCommRiskMapper.insertImcCommRisk(imcCommRisk);
    }

    /**
     * 修改佣金计算险种明细
     *
     * @param imcCommRisk 佣金计算险种明细
     * @return 结果
     */
    @Override
    public int updateImcCommRisk(ImcCommRisk imcCommRisk) {
        imcCommRisk.setUpdateTime(DateUtils.getNowDate());
        return imcCommRiskMapper.updateImcCommRisk(imcCommRisk);
    }

    /**
     * 批量删除佣金计算险种明细
     *
     * @param ids 需要删除的佣金计算险种明细ID
     * @return 结果
     */
    @Override
    public int deleteImcCommRiskByIds(Long[] ids) {
        int i =0;
        for (Long id : ids) {
            ImcCommRisk imcCommRisk = new ImcCommRisk();
            imcCommRisk.setId(id);
            imcCommRisk.setIsDeleted(id.toString());
            i+= imcCommRiskMapper.updateImcCommRisk(imcCommRisk);
        }
        return i;
    }

    /**
     * 删除佣金计算险种明细信息
     *
     * @param id 佣金计算险种明细ID
     * @return 结果
     */
    @Override
    public int deleteImcCommRiskById(String id) {
        return imcCommRiskMapper.deleteImcCommRiskById(id);
    }

    @Override
    public List<CommRiskVo> selectCommRiskVoList(CommRiskForm commRiskForm) {
        commRiskForm.setTenantId(SecurityUtils.getTenantId());
        return imcCommRiskMapper.selectCommRiskVoList(commRiskForm);
    }

    /**
     * 新增保单查询 todo
     * @param policyCode
     * @return
     */
    @Override
    public List<CommRiskVo> commOrder(String policyCode,Long commId) {
        return imcOrderRiskMapper.commOrder(policyCode,SecurityUtils.getTenantId(),null,commId);
    }

    /**
     * 添加保单到险种明细表
     * @param orderRiskIds
     * @param commId 佣金主表id
     * @return
     */
    @Transactional
    @Override
    public int addToCommRisk(Long commId,List<Long> orderRiskIds) {
        int i = 0;
        for (Long orderRiskId : orderRiskIds) {
            List<OrderRiskToCommRiskVo> commRiskVos = imcOrderRiskMapper.selectOrderRiskToCommRisk(orderRiskId);
            for (OrderRiskToCommRiskVo orderRisk : commRiskVos) {
                //查询order_risk_id是否保存过
                ImcCommRisk imcCommRiskTemp= imcCommRiskMapper.selectImcCommRiskByOrderRiskId(orderRisk.getId(),commId);
                if(imcCommRiskTemp!=null){
                    throw new BaseException("保单险种ID:"+orderRiskId+",已经保存，请重新选择保存");
                }
                ImcCommRisk imcCommRisk = new ImcCommRisk();
                BeanUtils.copyProperties(orderRisk,imcCommRisk);
                imcCommRisk.setCommId(commId);
                imcCommRisk.setIsAppend("Y");
                imcCommRisk.setOrderRiskId(orderRisk.getId());
                imcCommRisk.setCreateId(SecurityUtils.getUsername());
                imcCommRisk.setCreateTime(DateUtils.getNowDate());
                imcCommRisk.setUpdateId(SecurityUtils.getUsername());
                imcCommRisk.setUpdateTime(DateUtils.getNowDate());
                imcCommRisk.setTenantId(SecurityUtils.getTenantId());
                i+=imcCommRiskMapper.insertImcCommRisk(imcCommRisk);
            }
        }
        return i;
    }

    /**
     * 校验佣金主表状态是否为待计算
     * @param commId
     * @return
     */
    public boolean checkCommStatus(Long commId) {
        ImcComm imcComm = imcCommMapper.selectImcCommById(commId);
        return "A".equals(imcComm.getStatus());
    }
}
